home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / PCASM2.ZIP / CHAP1.DOC < prev    next >
Text File  |  1990-06-21  |  25KB  |  616 lines

  1.  
  2.  
  3.                                                                              1
  4.  
  5.                              CHAPTER 1 - SOME SIMPLE PROGRAMS
  6.  
  7.  
  8.              It is now time to start writing assembler code. One of the
  9.              problems with writing in assembler is that there is no way to get
  10.              input into the program or output from the program until you are
  11.              very far along with learning assembler language. This is a
  12.              Catch-22 situation. You can't learn assembler easily without
  13.              access to input and output, and you can't write i/o routines till
  14.              you know assembler.{1}  Help is at hand. Included on this disk is
  15.              a file called asmhelp.obj. It is actually a series of programs
  16.              that will allow you to get input from the keyboard and print
  17.              output to the screen. It has some other features which will be
  18.              explained later.
  19.  
  20.              The second problem at the start is that every assembler program
  21.              has a lot of overhead. These are standard instructions and
  22.              formats that you need to get the program to work AT ALL. This
  23.              disk contains templates that contain all the overhead, so to
  24.              write a program you just make a copy of the template and enter
  25.              the code and data at the appropriate place. By the end of this
  26.              sequence of lessons, you will know how to make templates yourself
  27.              and know the meaning of each word in the template. For now, you
  28.              have to have faith that what is written is necessary, and that
  29.              you will learn the meaning of everything later.
  30.  
  31.              Let's start. At the end of this chapter is the template we will
  32.              use for now - temp1.asm. These templates are in the subdirectory
  33.              \template. 
  34.  
  35.              Let's call the first program prog1.asm (very original). All
  36.              programs in assembler must have the file extension .asm so make a
  37.              copy by giving the command:
  38.  
  39.                  >copy \template\temp1.asm  prog1.asm
  40.  
  41.              You are now ready to enter code. Open up prog1.asm with your
  42.              editor, and take a look at it. It should look the same as
  43.              temp1.asm. 
  44.  
  45.              Where it says "put name of program here" - that is for your
  46.              personal use so you can see the program name while in the editor.
  47.              The assembler ignores everything after a semicolon. All the lines
  48.              that start with a semicolon are there for visual separation or
  49.              for comments. The lines with asterisks separate segments. Yes,
  50.              the assembler is going to make this program into three segments.
  51.              You should put all code between the line labeled "START CODE
  52.              BELOW THIS LINE" and the line labeled "END CODE ABOVE THIS LINE".
  53.              ____________________
  54.  
  55.                   1 Just to give you an idea of how contradictory the
  56.              situation is, asmhelp.obj was written in assembler language and
  57.              consists of about 3500 lines (that's about 50 pages), yet you
  58.              need to be using it from day one.
  59.  
  60.              ______________________
  61.  
  62.              The PC Assembler Tutor - Copyright (C) 1989 Chuck Nelson
  63.  
  64.  
  65.  
  66.  
  67.              The PC Assembler Tutor                                          2
  68.              ______________________
  69.  
  70.              Later you will get more flexibility. 
  71.  
  72.              Also notice the lines starting with the word EXTRN. Those lines
  73.              tell the assembler that the subroutines (such as print_num) are
  74.              in a different file and must be found when this program is
  75.              linked. The assembler enters each EXTRN name in a list and
  76.              records each place that an EXTRN subroutine is requested. It is
  77.              possible that one of these subroutines is never requested. That's
  78.              fine. However, every one of these subroutines must be present at
  79.              link time or you will get a link error. This is true even if the
  80.              subroutine is on the list but never requested. 
  81.  
  82.              Since the overhead is so long, and I am so lazy, the template
  83.              will never be included in the description of the program. What
  84.              you will get is the name of the template, then any data you need,
  85.              then the code. It will look like this:
  86.  
  87.              TEMP1.ASM
  88.              ; - - - - - START DATA BELOW THIS LINE
  89.              the data is written here
  90.              ; - - - - - END DATA ABOVE THIS LINE
  91.  
  92.              ; - - - - - START CODE BELOW THIS LINE
  93.              the code is written here
  94.              ; - - - - - END CODE ABOVE THIS LINE
  95.               
  96.  
  97.              If there is no data, no data section will be included. If there
  98.              is data, it should be written in the segment named DATASTUFF
  99.              between the lines "START DATA..." and "END DATA ...". The code
  100.              should be written between the lines that say "START CODE ..." and
  101.              "END CODE ...".
  102.  
  103.              For our first program, the description will look like this:
  104.  
  105.              TEMP1.ASM
  106.              ; - - - - - START CODE BELOW THIS LINE
  107.  
  108.              first_label:
  109.                  call get_num
  110.                  call print_num
  111.                  jmp  first_label
  112.  
  113.              ; - - - - - END CODE ABOVE THIS LINE
  114.  
  115.              That's all we need. If we needed to write all the overhead
  116.              (starting with the line "main proc far") we would have:
  117.  
  118.              main     proc far
  119.              start:   push ds
  120.                       sub  ax, ax
  121.                       push ax
  122.  
  123.                       mov  ax, DATASTUFF
  124.                       mov  ds, ax
  125.  
  126.              ; + + + + + + + + + + +
  127.  
  128.  
  129.  
  130.  
  131.              Chapter 1 - Some Simple Programs                                3
  132.              ________________________________
  133.  
  134.  
  135.              first_label:
  136.                  call get_num
  137.                  call print_num
  138.                  jmp  first_label
  139.  
  140.              ; + + + + + + + + + + +
  141.  
  142.                  ret
  143.  
  144.              mainendp
  145.              etc.
  146.  
  147.              You can see that a simple four line program has blossomed into a
  148.              monster. I'm assuming some intelligence on your part. Until
  149.              further notice, the code goes between the "START CODE' and the
  150.              "END CODE" lines and the data goes in the DATASTUFF segment
  151.              between the "START DATA" and the "END DATA" lines. 
  152.  
  153.              It's time to type in the program listed above. Be careful when
  154.              you type. When you are done I'll explain it. 
  155.  
  156.              In assembler we need a way to label different spots in the code.
  157.              We use labels. A LABEL is a name (at the beginning of a line)
  158.              which is immediately followed by a colon. A label doesn't
  159.              generate any code. The assembler merely keeps track of where the
  160.              label is for future use. The label we are using is named
  161.              first_label. 
  162.  
  163.              The CALL instruction tells the assembler to call the subroutine
  164.              listed after the call.{2} We are calling two subroutines; first
  165.              get_num which gets a number, then print_num, which prints a
  166.              number in a variety of styles. 
  167.  
  168.              Finally, JMP tells the assembler that you want to jump to the
  169.              label listed after it. It is the same as GOTO in BASIC. 
  170.  
  171.              If you look at the program, you will notice that we have an
  172.              infinite loop. It was designed that way. It takes a fair amount
  173.              of code to exit gracefully, so we will always exit ungracefully.
  174.              When you are tired of the program, simply press Control-C. That
  175.              should get you out. That way you can try out something an
  176.              indefinite number of times, and when you have finished you can
  177.              press CTRL-C to quit the program.
  178.  
  179.              One warning about machine language before we start. There is no
  180.              safety net, so before you start a machine language program, make
  181.              sure all files are closed (i.e. that you have no other programs
  182.              in memory). We will NEVER open a file in one of our programs.
  183.              ____________________
  184.  
  185.                   2 I am using the words subroutine, routine, program and
  186.              procedure (the technical word) interchangebly throughout the
  187.              book. A program is actually a group of one or more procedures,
  188.              but I'm not going to be too strict about it. Context should tell
  189.              whether we are talking about a single procedure or a whole
  190.              program.
  191.  
  192.  
  193.  
  194.  
  195.              The PC Assembler Tutor                                          4
  196.              ______________________
  197.  
  198.  
  199.              Your programs are almost certain to wind up in zombie space from
  200.              time to time. If that happens, your choices are (1) hit CTRL-C.
  201.              If that doesn't work, then (2) hit CTRL-ALT-DEL. As a last
  202.              resort, you can (3) hit a reset button or shut the machine down. 
  203.  
  204.              For that reason, memorize this mantra: BACKUP YOUR PROGRAMS AND
  205.              BACKUP YOUR BACKUPS.
  206.  
  207.              Double check that you have typed in the assembler code correctly.
  208.              Now it's time to assemble it. I am assuming that you have the
  209.              Microsoft assembler.{3}  Type: 
  210.  
  211.                  >masm  prog1 ;
  212.  
  213.              The first thing you will see is the copyright notice:
  214.  
  215.                 Microsoft (R) Macro Assembler Version 5.10
  216.                 Copyright (C) Microsoft Corp 1981, 1988.  All rights reserved. 
  217.                
  218.  
  219.              The file extension .asm is unnecessary; it's understood.
  220.              The semicolon is to speed things up. If you don't use it, the
  221.              assembler will ask you if you want to change any of the default
  222.              choices. If you type just masm:
  223.  
  224.                  >masm
  225.  
  226.              then you need to give the name of the assembler text file on the
  227.              first line:
  228.  
  229.                  Source filename [.ASM]: prog1
  230.                  Object filename [prog1.OBJ]:
  231.                  Source listing  [NUL.LST]:
  232.                  Cross-reference [NUL.CRF]:
  233.  
  234.              but press ENTER for the other options. If you type  masm prog1: 
  235.  
  236.                  >masm prog1
  237.  
  238.              You don't want to change any of the default settings:
  239.  
  240.                  Object filename [prog1.OBJ]:
  241.                  Source listing  [NUL.LST]:
  242.                  Cross-reference [NUL.CRF]:
  243.  
  244.              When the assembler asks you about options, hit the ENTER key.
  245.  
  246.              If you have made any errors, the assembler will tell you which
  247.              line they are on and give you a description of the problem. Make
  248.              a hard copy of them on the printer, then use your editor and find
  249.              the line. Unfortunately, at this stage of the game, it will be
  250.              ____________________
  251.  
  252.                   3 If you are using A86, then consult A86.APP. If you are
  253.              using Turbo Assembler, then consult TASM.APP. They are both
  254.              located in the \APPENDIX subdirectory on disk 3.
  255.  
  256.  
  257.  
  258.  
  259.              Chapter 1 - Some Simple Programs                                5
  260.              ________________________________
  261.  
  262.              very difficult for you to figure out what the problem is. You
  263.              will have to struggle through the first 4 or 5 programs before
  264.              things start getting easier. All the programs on these disks have
  265.              been compiled on a Microsoft v5.1 assembler. They have assembled.
  266.              They have been run, and they work. Don't tamper with the template
  267.              and copy the code exactly and everything should work.
  268.  
  269.              If you haven't made any errors, the assembler will say:
  270.  
  271.                    0 Warning Errors 
  272.                    0 Severe  Errors 
  273.  
  274.  
  275.  
  276.              LINKING
  277.  
  278.              The assembler has given you back another program named prog1.obj
  279.              - the same name with the extension .obj. I am assuming that you
  280.              have all used the linker with compiled programs. If you haven't,
  281.              you may be getting in over your head by using machine language. 
  282.              All the extra subroutines are in a program called asmhelp.obj.
  283.              Its pathname is \asmhelp\asmhelp.obj. You want to put it in the
  284.              root directory of your current drive. In the whole book, we will
  285.              assume that its pathname is:
  286.  
  287.                  \asmhelp.obj
  288.  
  289.              If you put it somewhere else, you will have to modify the
  290.              pathname whenever it appears. Link the two modules by writing:
  291.  
  292.                  >link prog1+\asmhelp ;
  293.  
  294.              The copyright notice will appear:
  295.  
  296.                 Microsoft (R) Overlay Linker  Version 3.61
  297.                 Copyright (C) Microsoft Corp 1983-1987.  All rights reserved. 
  298.                 
  299.              This time the file extensions are understood to be .obj. The
  300.              semicolon is to avoid having to make default choices. If you
  301.              type:
  302.  
  303.                  >link
  304.  
  305.              then you need to put the module names after the first prompt: 
  306.  
  307.                  Object Modules [.OBJ]: prog1+\asmhelp               
  308.                  Run File [PROG1.EXE]:               
  309.                  List File [NUL.MAP]:               
  310.                  Libraries [.LIB]: 
  311.  
  312.              but press ENTER for the other choices. If you type:
  313.  
  314.                  link prog1+\asmhelp
  315.  
  316.              You need to do nothing extra:
  317.  
  318.                  Run File [PROG1.EXE]:               
  319.  
  320.  
  321.  
  322.  
  323.              The PC Assembler Tutor                                          6
  324.              ______________________
  325.  
  326.                  List File [NUL.MAP]:               
  327.                  Libraries [.LIB]:               
  328.  
  329.              When the linker asks for choices, simply press the ENTER key.
  330.  
  331.              The linker gives the executable file the name of the first object
  332.              file on the line, so you should always put your program first and
  333.              asmhelp.obj second.
  334.  
  335.              If there are no errors, you are ready to go. If there are errors,
  336.              once again, they will be very difficult to trace. Go back and
  337.              check everything from the beginning.
  338.  
  339.              You are now ready to run the program. Type:
  340.  
  341.                  >prog1
  342.  
  343.              The program will start. The first thing you will see is a
  344.              copyright notice. 
  345.  
  346.                  The PC Assembler Helper   Version 1.0
  347.                  Copyright (C) 1989  Chuck Nelson   All rights reserved.
  348.  
  349.              It appears the first time you call a subprogram in the module
  350.              asmhelp.obj.
  351.  
  352.              The program will request a number. Give it any legal signed or
  353.              unsigned number. It should be no longer than 5 digits. Press
  354.              ENTER, and it will display the possible ways that that number can
  355.              be thought of by the computer. 
  356.  
  357.                  Enter any decimal number  4410
  358.                    HEX    SIGNED   UNSIGNED   CHAR          BINARY
  359.                   113AH   +04410    04410     11  : **  0001000100111010
  360.  
  361.                  Enter any decimal number  30486
  362.                    HEX    SIGNED   UNSIGNED   CHAR          BINARY
  363.                   7716H   +30486    30486      w 16 **  0111011100010110
  364.  
  365.              If the signed or unsigned number doesn't look the same as what
  366.              you entered, then the number you entered is too big for a 16 bit
  367.              computer. For signed numbers, the limits are +32767 to -32768 and
  368.              for unsigned numbers, the limits are 0 to 65535.
  369.  
  370.                  Enter any decimal number  -64661
  371.                    HEX    SIGNED   UNSIGNED   CHAR          BINARY
  372.                   036BH   +00875    00875     03  k **  0000001101101011
  373.  
  374.                  Enter any decimal number  94547
  375.                    HEX    SIGNED   UNSIGNED   CHAR          BINARY
  376.                   7153H   +29011    29011      q  S *   0111000101010011
  377.  
  378.  
  379.              Lets look at the numbers. Each type of output is labeled. After a
  380.              hex number, there is an 'H' and after the characters, there is a
  381.              '*'. This is always true. Every time you print a hex number,
  382.              there will be an 'H', and every time you print a character, there
  383.  
  384.  
  385.  
  386.  
  387.              Chapter 1 - Some Simple Programs                                7
  388.              ________________________________
  389.  
  390.              will be a '*'. This is so you will always know what is being
  391.              printed. Also notice that a signed integer ALWAYS has a sign and
  392.              an unsigned integer NEVER has a sign.
  393.  
  394.              Not all characters are visible. Ascii 0 - 32 are invisible (32 is
  395.              a blank). On the PC, ascii 33-255 are visible, but ascii 127 and
  396.              ascii 255 are problematic. Therefore, if the ascii code is 0-32,
  397.              127 or 255, that character will be printed as a hex number, not a
  398.              character, and print_num will signal the event by printing a
  399.              double asterisk '**' instead of a single one. This has happened
  400.              in the first two examples. ( 11  : ** )  and  (  w 16 ** ). The
  401.              first one is the hex number 11 followed by the character ':' and
  402.              the second one is the character 'w' followed by the hex number
  403.              16. Both are signalled by the double asterisk '**' instead of the
  404.              single asterisk '*'.
  405.  
  406.              Do a few examples. When you are done looking at the numbers,
  407.              press CTRL-C and you will exit the program.
  408.  
  409.                  Enter any decimal number  ^C              
  410.  
  411.  
  412.              PROGRAM 2
  413.  
  414.              The second program is almost the same as the first one. The
  415.              program takes input from the keyboard and displays it in a
  416.              variety of styles. This time, however, it is going to ask for
  417.              different inputs: ascii, hex, binary and decimal. If you make an
  418.              error in the input, the subroutine will prompt you again for the
  419.              input. Here's the program:
  420.  
  421.  
  422.              TEMP1.ASM
  423.              ;+ + + + + + + + + + START CODE BELOW THIS LINE
  424.  
  425.              first_label:
  426.                  call get_num             ; 1 to 5 digit signed or unsigned
  427.                  call print_num
  428.  
  429.                  call get_ascii           ; 1 or 2 characters
  430.                  call print_num
  431.  
  432.                  call get_binary          ; a 1 to 16 bit binary number
  433.                  call print_num
  434.  
  435.                  call get_hex             ; a 1 to 4 digit hex number
  436.                  call print_num
  437.  
  438.                  jmp  first_label
  439.  
  440.              ;+ + + + + + + + + + END CODE ABOVE THIS LINE
  441.  
  442.              The things to the right of the semicolons are comments. You do
  443.              not need to type them in if you don't want to. Once again,
  444.              assemble the program. (There should be no warning or severe
  445.              errors. If something is wrong, it is most likely a typing error.)
  446.              Then link it with asmhelp.obj. Remember - your program should be
  447.  
  448.  
  449.  
  450.  
  451.              The PC Assembler Tutor                                          8
  452.              ______________________
  453.  
  454.              the first one listed.{4}
  455.  
  456.              If all is well, run the program. It will ask you for a number
  457.              (that is a signed or unsigned number), ascii characters, a binary
  458.              number, and a 4 digit hex number (0-9,A-F). 
  459.  
  460.                  Enter any decimal number  27959
  461.                    HEX    SIGNED   UNSIGNED   CHAR          BINARY
  462.                   6D37H   +27959    27959      m  7 *   0110110100110111
  463.  
  464.                  Enter one or two ascii characters  $%
  465.                    HEX    SIGNED   UNSIGNED   CHAR          BINARY
  466.                   2425H   +09253    09253      $  % *   0010010000100101
  467.  
  468.                  Enter a two byte binary number  0101111001100010
  469.                    HEX    SIGNED   UNSIGNED   CHAR          BINARY
  470.                   5E62H   +24162    24162      ^  b *   0101111001100010
  471.  
  472.                  Enter a two byte hex number  784d
  473.                    HEX    SIGNED   UNSIGNED   CHAR          BINARY
  474.                   784DH   +30797    30797      x  M *   0111100001001101
  475.  
  476.  
  477.              Once again, this is an infinite loop, so in order to quit, you
  478.              need to hit CTRL-C.
  479.  
  480.              The purpose of these first two programs is to remind you that the
  481.              computer doesn't care whether you think you are storing binary
  482.              numbers, characters, hex numbers, signed numbers or unsigned
  483.              numbers. They all wind up in the computer as a series of 1s and
  484.              0s, and you can use these 1s and 0s any way you like. It's up to
  485.              you to keep track of them. 
  486.  
  487.              If you feel comfortable with the way we are writing, assembling
  488.              and linking programs, you are ready to start looking at the 8086
  489.              itself. 
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.              ____________________
  500.  
  501.                   4 For your convenience, there is a batch file on the disk
  502.              called asmlink.bat. Its pathname is \asmhelp\asmlink.bat. It is
  503.              one line long and looks like this:
  504.  
  505.                     link %1+\asmhelp ;
  506.  
  507.              If you use this batch file, you will never have an order problem.
  508.              If your file is named myfile.asm, then type:
  509.  
  510.                     >asmlink myfile
  511.  
  512.  
  513.  
  514.  
  515.              Chapter 1 - Some Simple Programs                                9
  516.              ________________________________
  517.  
  518.              ; TEMP1.ASM        The first assembler template
  519.  
  520.              ; put name of program here
  521.  
  522.              ; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  523.              STACKSEG    SEGMENT   STACK  'STACK'
  524.  
  525.                           dw     100 dup (?)
  526.  
  527.              STACKSEG    ENDS
  528.              ; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  529.              DATASTUFF    SEGMENT   PUBLIC  'DATA'
  530.  
  531.              ; + + + + + + + + + + + + + + + START DATA BELOW THIS LINE
  532.  
  533.              ; + + + + + + + + + + + + + + + END DATA ABOVE THIS LINE
  534.  
  535.              DATASTUFF    ENDS
  536.              ; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  537.              CODESTUFF    SEGMENT   PUBLIC  'CODE'
  538.  
  539.                     EXTRN  print_num:NEAR , get_num:NEAR 
  540.                     EXTRN  get_ascii:NEAR , get_hex:NEAR , get_binary:NEAR
  541.  
  542.                     ASSUME cs:CODESTUFF, ds:DATASTUFF
  543.  
  544.              main   proc far
  545.              start: push  ds               ; set up for return
  546.                     sub   ax,ax
  547.                     push  ax
  548.  
  549.                     mov   ax, DATASTUFF
  550.                     mov   ds,ax
  551.  
  552.              ; + + + + + + + + + + + + + + + START CODE BELOW THIS LINE
  553.  
  554.              ; + + + + + + + + + + + + + + + END CODE ABOVE THIS LINE
  555.  
  556.                     ret
  557.  
  558.              main   endp
  559.  
  560.              CODESTUFF    ENDS
  561.              ; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  562.  
  563.                     END     start
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.              The PC Assembler Tutor                                         10
  580.              ______________________
  581.  
  582.                             SUMMARY
  583.  
  584.              CALL
  585.                  CALL calls a subroutine.
  586.  
  587.                       call get_num
  588.  
  589.              JMP
  590.                  JMP jumps to the indicated label.
  591.  
  592.                       JMP  label47
  593.  
  594.              LABEL
  595.                  A label is a name at the beginning of a line which is
  596.                  followed by a colon. It is used to mark a spot in the
  597.                  program.
  598.  
  599.                       label47:
  600.  
  601.  
  602.              There are three different things which will be mentioned from
  603.              time to time, so it's best to define them now.
  604.  
  605.              ASSEMBLER INSTRUCTIONS (CODE) is the text that you type in and
  606.              give to the assembler.
  607.  
  608.              MACHINE CODE is the code that the assembler generates. After some
  609.              adjustment by the linker, it is readable by the 8086. It is the
  610.              actual code that controls the program.
  611.  
  612.              MICROCODE is the code that is imbedded in the 8086 itself. Each
  613.              instruction has its own set of mini instructions within the 8086.
  614.              This is the MICROCODE.
  615.  
  616.